package org.grow.secure.filter;

import org.springframework.stereotype.Service;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @Author: xwg
 * @CreateDate: 21-7-15
 */

@Service
public class DebugFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest,
                                    HttpServletResponse httpServletResponse,
                                    FilterChain filterChain) throws ServletException, IOException {
        Cookie[] cookies = httpServletRequest.getCookies();


        if (cookies == null) {
            System.out.println("浏览器没有发送任何cookie，本身为null");
        } else if (cookies.length == 0) {
            System.out.println("浏览器没有发送任何cookie,不包含元素");
        } else {

            Arrays.stream(cookies)
                    .forEach(x -> {
                        System.out.println(" cookie : " + x.getName() + " ==> " + x.getValue());
                    });
        }

        System.out.println("字符编码集" + httpServletRequest.getCharacterEncoding()
                + " == " + httpServletResponse.getCharacterEncoding());
        filterChain.doFilter(httpServletRequest, httpServletResponse);

    }
}
